<?php

class HtmlExportModel extends CFormModel {
	
	public $theme = 'default';
	public $themePath = 'dbMonitor.views.htmlExport.themes';
	public $docPath = 'application.docs.database';
	public $docName = 'manual';
	public $docTitle = 'Database Table Manual';
	public $themeCharset = 'GB2312';
	
	protected $_realThemePath = null;
	protected $_realDocPath = null;
	
	protected $_controller = null;
	
	public function attributeLabels() {
		return array(
			'theme' => '皮肤名',
			'themePath' => '皮肤目录',
			'themeCharset' => '皮肤字符集',
			'docPath' => '文档路径',
			'docTitle' => '文档窗口标题',
			'docName' => '文档名称'
		);
	}
	
	public function rules() {
		return array(
			array('theme, themePath, docPath, docTitle, docName', 'required'),
			array('themeCharset', 'safe'),
		);
	}
	
	public function getDb() {
		return DbmDb::getInstance();
	}
	
	public function getPackages() {
		return $this->getDb()->getPackages();
	}
	
	public function getTables() {
		return $this->getDb()->getTables();
	}
	
	public function getController() {
		if(!$this->_controller) {
			$this->_controller = new CController(null, new DbMonitorModule(null, Yii::app()));
		}
		return $this->_controller;
	}
	
	public function export() {
		if($this->validate()) {
			$tables = $this->getTables();
			$docPath = $this->getRealDocPath();
			$themePath = $this->getRealThemePath();
			$docName = $this->docName;
			
			@mkdir($docPath, 0777, true);
			CFileHelper::copyDirectory(
				$themePath.'/assets',
				$docPath,
				array('exclude'=>array('.svn'))
			);
			$content = $this->render('index', array('model'=>$this), true);
			file_put_contents($docPath.'/index.html',$this->convContent($content));
			
			foreach($this->getTables() as $table) {
				$content = $this->render('table', array('model'=>$this, 'table'=>$table), true);
				file_put_contents($docPath."/{$table->table_name}.html",$this->convContent($content));
			}
			
			$content=$this->renderPartial('chmProject',array('model'=>$this),true);
			file_put_contents($docPath."/{$docName}.hhp",$this->convContent($content));
	
			$content=$this->renderPartial('chmIndex',array('model'=>$this),true);
			file_put_contents($docPath."/{$docName}.hhk",$this->convContent($content));
	
			$content=$this->renderPartial('chmContents',array('model'=>$this),true);
			file_put_contents($docPath."/{$docName}.hhc",$this->convContent($content));
			return true;	
		}
		return false;
	}
	
	public function convContent($content) {
		if($this->themeCharset !== 'UTF-8') {
			return iconv('UTF-8', $this->themeCharset.'//IGNORE', $content);
		}
		return $content;
	}

	public function getRealThemePath() {
		if(!$this->_realThemePath) {
			$this->_realThemePath = $this->resolvePath($this->themePath) . '/' . $this->theme;
		}
		return $this->_realThemePath;
	}
	
	public function getRealDocPath() {
		if(!$this->_realDocPath) {
			$this->_realDocPath = $this->resolvePath($this->docPath);
		}
		return $this->_realDocPath;
	}
	
	public function resolvePath($path) {
		return strpos($this->themePath, '.')!==false ? Yii::getPathOfAlias($path) : $path;
	}
	
	public function render($view,$data=null,$return=false,$layout='main')
	{
		$viewFile=$this->getRealThemePath()."/views/{$view}.php";
		$layoutFile=$this->getRealThemePath()."/layouts/{$layout}.php";
		$content=$this->controller->renderFile($viewFile,$data,true);
		return $this->controller->renderFile($layoutFile,array('content'=>$content),$return);
	}

	public function renderPartial($view,$data=null,$return=false)
	{
		$viewFile=$this->getRealThemePath()."/views/{$view}.php";
		return $this->controller->renderFile($viewFile,$data,$return);
	}
	

}

?>